package com.syl.util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * Created by syl on 2016/6/18.
 */
public class DbHelp {
    private static final String CONF ="conf.xml";
    private static SqlSession session = null;

    public DbHelp(){
        session = getSession();
    }

    public SqlSession getSession(){
        InputStream is = DbHelp.class.getClassLoader().getResourceAsStream(CONF);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return sessionFactory.openSession();
    }

    public <T> T getDao(Class<T> type) {
        return session.getConfiguration().getMapper(type, session);
    }

    public void commit(){
        session.commit();
        if(session != null) session.close();
    }

}
